// pages/lifeCircle/lifeCircle.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pxTorpx: 2, //px转化为rpx的系数
    leftLooks: 660, //rpx
    topLooks: 790, //rpx
    widthScreen: null,
    heightScreen: null,
    nowSkinUrl: "",
    nowColor: "#fff",
    allPeople: 1000
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var nowColor = app.globalData.nowColor;
    that.setData({
      nowSkinUrl: app.globalData.nowSkinUrl,
      nowColor: nowColor
    });
    that.getFirstCircle();
    var pxTorpx = that.data.pxTorpx;
    //获取屏幕高宽
    wx.getSystemInfo({
      success: function(res) {
        //rpx
        that.setData({
          widthScreen: res.windowWidth * (750 / res.windowWidth),
          heightScreen: res.windowHeight * (750 / res.windowWidth) + 300,
          pxTorpx: (750 / res.windowWidth)
        });
      }
    });
  },

  //搜索 搜集formID
  onToSearchPage: function(e) {
    wx.navigateTo({
      url: '/pages/lifeCircle/searchCircle'
    })
    //搜集formID
    var user_id = wx.getStorageSync('user_id');
    var formId = e.detail.formId;
    app.saveFormId(user_id, formId);
  },

  //搜索进入入口
  onSeacrch: function() {
    wx.navigateTo({
      url: '/pages/lifeCircle/searchCircle'
    })
  },

  //点击圈子，进入圈子页面
  onToPostListPage: function(e) {
    var discuss_id = e.currentTarget.dataset.discuss_id;
    var judge = e.currentTarget.dataset.judge;
    if (judge) {
      judge = 1;
    } else {
      judge = 0
    }
    wx.navigateTo({
      url: '/pages/lifeCircle/postList?discuss_id=' + discuss_id + '&&judge=' + judge,
    })
  },

  onShow: function() {
    var that = this;
    var allPeople = wx.getStorageSync('allPeople');
    that.setData({
      allPeople: allPeople
    })
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      that.setData({
        attentionDisabled: true
      })
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }
        }
      })
    } else {
      that.setData({
        nowSkinUrl: app.globalData.nowSkinUrl,
        nowColor: app.globalData.nowColor
      });
      that.setData({
        attentionDisabled: false
      })
      that.getCircle(); //全部圈子
    }
    that.setData({
      nowSkinUrl: app.globalData.nowSkinUrl
    });
  },


  //已关注列表
  onGetCircleList: function() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.targetUrl + 'discuss/collect/',
      data: {
        user_id: user_id
      },
      success: res => {
        var circleList = res.data.data;
        var discuss_id_list = that.data.discuss_id_list;
        var attention = that.data.attention;
        for (var i = 0; i < discuss_id_list.length; i++) {
          if (circleList.length == 0) {
            attention[i] = false;
          }
          for (var j = 0; j < circleList.length; j++) {
            if (circleList[j].discuss_id == discuss_id_list[i] || attention[i] == true) {
              attention[i] = true;
            } else {
              attention[i] = false;
            }
          }
        }
        that.setData({
          circleList: circleList,
          attention: attention
        })
      }
    })
  },

  //读取帖子数据
  getCircle: function() {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'discuss/',
      method: "GET",
      success: function(res) {
        var list = res.data.data;
        console.log(list);
        that.setData({
          list: list,
        })
        that.onGetCircleList(); //已关注圈子
      }
    })
  },

  //第一次读取帖子数据,初始化attention数组
  getFirstCircle: function() {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'discuss/',
      method: "GET",
      success: function(res) {
        var list = res.data.data;
        var attention = [];
        var discuss_id_list = [];
        for (var i = 0; i < list.length; i++) {
          discuss_id_list[i] = list[i].discuss_id;
          attention[i] = false;
        }
        that.setData({
          list: list,
          attention: attention,
          discuss_id_list: discuss_id_list
        })
      }
    })
  },

  //点击按钮“关注” 并收集formID
  onCircleCollect: function(e) {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var index = e.currentTarget.dataset.id;
    var discuss_id = e.currentTarget.dataset.discuss_id;
    var attention = that.data.attention;
    if (attention[index]) {
      attention[index] = false;
      that.setData({
        attention: attention
      })
      wx.request({
        url: app.globalData.targetUrl + 'discuss/collect/',
        //method: 'DELETE',
        method: 'GET',
        data: {
          user_id: user_id,
          discuss_id: discuss_id,
          is_delete: 1
        },
        success: res => {
          that.getCircle(); //刷新
        }
      })
    } else {
      attention[index] = true;
      that.setData({
        attention: attention
      })
      wx.request({
        url: app.globalData.targetUrl + 'discuss/collect/',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          user_id: user_id,
          discuss_id: discuss_id
        },
        success: res => {
          that.getCircle(); //刷新
        }
      })
    }
  },

  //小松鼠动画
  ClickFairy: function() {
    var that = this;
    var animation = wx.createAnimation({});
    var time = 440;
    var widthScreen = this.data.widthScreen;
    var heightScreen = this.data.heightScreen;
    var leftLooks = that.data.leftLooks;
    var topLooks = that.data.topLooks;
    var pxTorpx = that.data.pxTorpx;
    animation.translate(((widthScreen / 2 - leftLooks) - 20) / pxTorpx, ((heightScreen / 2 - topLooks) - 20) / pxTorpx).scale(0).opacity(0.4).step({
      duration: time
    })
    this.setData({
      queueData: animation.export()
    })
    this.sleep(time);
    wx.navigateTo({
      url: '/pages/launchTopic/sendPOST',
      success: res => {
        animation.translate(0, 0).scale(1).opacity(1).step({
          duration: time
        })
        that.setData({
          queueData: animation.export()
        })
      }
    })
  },

  imagetouchmove: function(e) {
    var that = this;
    var pxTorpx = that.data.pxTorpx;
    var left = e.touches[0].clientX * pxTorpx - 45; //rpx
    var top = e.touches[0].clientY * pxTorpx - 45; //rpx
    if (left < 0 && left) {
      left = 0
    };
    if (left > that.data.widthScreen - 90) {
      left = that.data.widthScreen - 90
    };
    if (top < 0) {
      top = 0
    };
    if (top > that.data.heightScreen - 190) {
      top = that.data.heightScreen - 190
    };

    that.setData({
      leftLooks: left,
      topLooks: top
    })
  },
  imagetouchend: function(e) {
    var that = this;
    var animation = wx.createAnimation({});
    var widthScreen = this.data.widthScreen;
    var left_before = that.data.leftLooks;
    if (left_before == that.data.widthScreen - 90) return;
    var left = that.data.widthScreen - 90;
    that.setData({
      leftLooks: left
    })
    var time = 300;
    var n1 = left_before / widthScreen; //形变系数
    if (n1 < 0.2) {
      n1 = 0.2
    }
    var n2 = (widthScreen - left_before) / widthScreen * 0.7;
    var pxTorpx = that.data.pxTorpx;
    console.log(pxTorpx);
    console.log("n1:", 1 - n1, (90 / pxTorpx) * (1 - n1));
    animation.scale(n1, 1 + n2).translate((90 / pxTorpx) * (1 - n1), 0).step({
      duration: time
    });
    that.setData({
      queueData: animation.export()
    });
    that.sleep(time);
    animation.scale(1, 1).translate(0, 0).step({
      duration: time
    });
    that.setData({
      queueData: animation.export()
    });

  },

  //延时函数
  sleep: function(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
      now = new Date();
      if (now.getTime() > exitTime)
        return;
    }
  },
})